package com.example.tvfoucssustom.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.tvfoucssustom.R;
import com.example.tvfoucssustom.base.BaseFragment;
import com.owen.focus.FocusBorder;

/**
 * Created by dasu on 2017/4/20.
 * <p>
 * 各类型干货的数据展示界面，
 */

public class YinyueFragment extends BaseFragment implements View.OnFocusChangeListener {
    private static final String TAG = YinyueFragment.class.getSimpleName();
    private TextView tvView;
    private GridLayout gridLayout;
    private RelativeLayout flFigure1;
    private RelativeLayout flFigure2;
    private RelativeLayout flFigure3;
    private RelativeLayout flTime1;
    private RelativeLayout flTime2;
    private RelativeLayout flTimeMore;
    private FrameLayout flCloud;
    private FrameLayout flAddress1;
    private FrameLayout flAddress2;
    private FrameLayout flAddressMore;
    private RelativeLayout flAllImage;
    private FrameLayout flElePhoto;

    public YinyueFragment() {
    }

    private Context mContext;


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.mContext = context;
//        DebugLog.d(LOG+TAG + " ---- onAttach");

    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//        DebugLog.d(LOG+TAG + " ---- onCreateView");
        View view = inflater.inflate(R.layout.fragment_photo, container, false);
        initView(view);
        return view;
    }

    /**
     * 判断是不是在前台的控件
     * @return
     */
    public boolean isBefore(){
       return flFigure1.hasFocus()||flFigure2.hasFocus()||flTime1.hasFocus()||flTime2.hasFocus()||flCloud.hasFocus();
    }

    public boolean isALLBefore(){
       return flFigure1.hasFocus()||flFigure2.hasFocus()||flTime1.hasFocus()||flTime2.hasFocus()||flCloud.hasFocus();
    }

    private void initView(View view) {
        gridLayout = view.findViewById(R.id.tv_gridlayout);
        flFigure1 = view.findViewById(R.id.rlFigure1);
        flFigure2 = view.findViewById(R.id.rlFigure2);
        flFigure3 = view.findViewById(R.id.llFigureMore);
        flTime1 = view.findViewById(R.id.rlTime1);
        flTime2 = view.findViewById(R.id.rlTime2);
        flTimeMore = view.findViewById(R.id.llTimeMore);

        flCloud = view.findViewById(R.id.llCloud);
        flAddress1 = view.findViewById(R.id.rlAddress1);
        flAddress2 = view.findViewById(R.id.rlAddress2);
        flAddressMore = view.findViewById(R.id.llAddressMore);
        flAllImage = view.findViewById(R.id.llAllImage);
        flElePhoto = view.findViewById(R.id.llElePhoto);

        flCloud.setOnFocusChangeListener(this);
        flAddress1.setOnFocusChangeListener(this);
        flAddress2.setOnFocusChangeListener(this);
        flAddressMore.setOnFocusChangeListener(this);
        flElePhoto.setOnFocusChangeListener(this);
        flAllImage.setOnFocusChangeListener(this);


        flFigure1.setOnFocusChangeListener(this);
        flFigure2.setOnFocusChangeListener(this);
        flFigure3.setOnFocusChangeListener(this);
        flTime1.setOnFocusChangeListener(this);
        flTime2.setOnFocusChangeListener(this);
        flTimeMore.setOnFocusChangeListener(this);


        mDrawableFocusBorder.boundGlobalFocusListener(new FocusBorder.OnFocusCallback() {
            @Override
            public FocusBorder.Options onFocus(View oldFocus, View newFocus) {
                if (null != newFocus) {
                    switch (newFocus.getId()) {
                        case R.id.rlFigure1:
                        case R.id.rlFigure2:
                        case R.id.llFigureMore:
                        case R.id.rlTime1:
                        case R.id.rlTime2:
                        case R.id.llCloud:
                        case R.id.llTimeMore:
                        case R.id.rlAddress1:
                        case R.id.rlAddress2:
                        case R.id.llAddressMore:
                        case R.id.llAllImage:
                        case R.id.llElePhoto:

                            return createDrawableFocusBorder(0);
                        default:
                            break;
                    }
                }
                mDrawableFocusBorder.setVisible(false);
                //返回null表示不使用焦点框框架
                return null;
            }
        });




    }


    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            v.bringToFront();
        }
    }
}
